/*******************************************************************************
版权声明: Copyright(C) HT-Tech Co., Ltd. 2020-2021. All rights reserved.
文件名称: htlist.c
文件描述: 链表操作
创 建 者: 陈位仅
创建时间: 2024年11月14日
修改历史:
1. 2024年11月14日    陈位仅      创建文件 
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "htlist.h"

// hash表
struct list_head *gHead = NULL;
// hash表成员数
unsigned int list_number = 0;

// 创建表
int list_init(int nmb)
{
    gHead = calloc(nmb, sizeof(struct list_head));
    if (NULL == gHead) {
        return -1;
    }
    list_number = nmb;

    return 0;
}

// 添加节点成员
int list_node_add(struct list_node *node, unsigned int index)
{
    struct list_head *list = NULL;

    if (index >= list_number) {
        return -1;
    }

    list = gHead + index;
    if (NULL == list->node) {
        list->node = node;
    } else {
        node->next = list->node;
        list->node = node;
    }

    return 0;
}

//添加节点成员到指定表
int list_node_insert(struct list_node *node, struct list_head *head)
{
    if (NULL == node || NULL == head) {
        return -1;
    }

    if (NULL == head->node) {
        head->node = node;
    } else {
        node->next = head->node;
        head->node = node;
    }

    return 0;
}

//删除节点成员
int list_node_del(void *data, CMP *func, int index)
{
    struct list_head *list = NULL;
    struct list_node *prev = NULL;
    struct list_node *node = NULL;
    
    if (index >= list_number || NULL == func || NULL == data) {
        return -1;
    }

    list = gHead + index;
    prev = list->node;
    node = list->node;
    for ( ; node != NULL; node = node->next) {
        if (0 == func(node->data, data, 0)) {
            if (list->node == node) {
                list->node = node->next;
            }else {
                prev->next = node->next;
            }
            break;
        }
        prev = node;
    }

    if (NULL == node) {
        return -1;
    }

    list_node_free(node);
    return 0;
}

//根据数据内容查找节点
struct list_node *list_node_get(int index, void *data, CMP *func, int flag)
{
    struct list_head *list = NULL;
    struct list_node *node = NULL;

    if (index >= list_number || NULL == func || NULL == data) {
        return NULL;
    }

    list = gHead + index;
    node = list->node;
    for ( ; node != NULL; node = node->next) {
        if (0 == func(node->data, data, flag)) {
            return node;
        }
    }

    return NULL;
}

//根据数据内容在指定表中查找节点
struct list_node *list_node_find(struct list_head *head, void *data, CMP *func, int flag)
{
    struct list_node *node = NULL;

    if (NULL == func || NULL == data) {
        return NULL;
    }

    for (node = head->node ; node != NULL; node = node->next) {
        if (node->data) {
             if (0 == func(node->data, data, flag)) {
                return node;
            }
        }
    }

    return NULL;
}

//获取指定序号的表
struct list_head *list_head_get(int index)
{
    if (index >= list_number) {
        return NULL;
    }

    return &gHead[index];
}

//使用已有数据创建结点（数据拷贝）
struct list_node *list_node_creat(void *data, int len)
{
    struct list_node *node = NULL;

    if (NULL == data) {
        return NULL;
    }

    node = (struct list_node *)calloc(1, sizeof(struct list_node));
    if(NULL == node) {
        return NULL;
    }

    node->data = calloc(1, len);
    if(NULL == node) {
        free(node);
        return NULL;
    }

    memcpy(node->data, data, len);
    node->next = NULL;
    return node;
}

//使用已有数据创建结点
struct list_node *list_node_creat_withdata(void *data)
{
    struct list_node *node = NULL;

    if (NULL == data) {
        return NULL;
    }

    node = (struct list_node *)calloc(1, sizeof(struct list_node));
    if(NULL == node) {
        return NULL;
    }

    node->data = data;
    node->next = NULL;
    return node;
}

//结点内存释放
void list_node_free(struct list_node *node)
{
    if (node) {
        if (node->data) {
            free(node->data);
            node->data = NULL;
        }
        
        free(node);
        node = NULL;
    }
}


//使用节点数据回收接口销毁表
void list_destory(DESTORY *func)
{
    int i = 0;
    struct list_head *list = NULL;
    struct list_node *node = NULL;
    struct list_node *safe = NULL;

    if (func) {
        for ( ; i < list_number; i++) {
            list = gHead + i;
            for (node = list->node; node != NULL; node = safe) {
                func(node->data);
                node->data = NULL;
                safe = node->next;
                list_node_free(node);
            }
        }
    } else {
        for ( ; i < list_number; i++) {
            list = gHead + i;
            for (node = list->node; node != NULL; node = safe) {
                free(node->data);
                node->data = NULL;
                safe = node->next;
                list_node_free(node);
            }
        }
    }
}

//使用节点数据回收接口销毁指定表
void list_head_destory(DESTORY *func, struct list_head *pHead)
{
    struct list_node *node = NULL;
    struct list_node *safe = NULL;

    if (func) {
        for (node = pHead->node; node != NULL; node = safe) {
            func(node->data);
            node->data = NULL;
            safe = node->next;
            list_node_free(node);
        }
    } else {
        for (node = pHead->node; node != NULL; node = safe) {
            free(node->data);
            node->data = NULL;
            safe = node->next;
            list_node_free(node);
        }
    }
}
